<?php
namespace Admin\Controller;

class OrderController extends AdminController {

	#列表
	public function index(){ 
        $order_no = I('get.order_no');
        $startDate = I('get.startDate');
        $endDate = I('get.endDate');
        $payStatus = I('get.payStatus');

        if ($payStatus!='') {
            $map['payStatus'] = $payStatus;
        }

        if ($order_no!='') {
            $map['order_no'] = $order_no;
        }

        if ($startDate!='' && $endDate!='') {
            $map['createTime'] = array('between',strtotime($startDate).','.strtotime($endDate));
        }

        $obj = M('Order');
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 20);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

	public function view() {
        $id = (int) $_GET['id'];
        $map['id']=$id;
        $obj = M('Order');
        $list = $obj->where("id=$id")->find();
        if (!$list) {
            $this->error('Information does not exist');
        } else {
            unset($map);
            $map['order_no'] = $list['order_no'];
            $shop = M('OrderDetail')->where($map)->select();
            $this->assign('list', $list);
            $this->assign('shop', $shop);
            $this->display();
        }
    }

	#更新订单
    public function update(){
        $data['status'] = I('post.status');
        if ($data['status']=='') {
            $data['status']=0;
        }else{
            $data['status']=1;
        }
        $data['remark'] = I('post.remark');
        $data['updateTime'] = time();
        $id= I('post.id');
        $list = M('Order')->where('id='.$id)->save($data);
        if ($list) {
            $state = 'SUCCESS';
            $message = 'Successful operation';
            $url = U('Order/index');
        }else{
            $state = L('_action_fail');
            $url = U('Order/index');
        }
        echo $this->echo_json_str($state,$message,$url);
    }

    public function change() {
        $state = I('get.state');
        $map['id']=I('get.id');
        $data['status']=$state;
        M('Order')->where($map)->save($data);
        redirect ($_SERVER['HTTP_REFERER']);
    }


    #删除废弃订单
    function del() {

        $id=I('post.selectedids');
        if($id==''){
            $state = "You do not select any information!";
        }else{
            foreach ($id as $v) {
                $obj = M('Order');
                $where['id'] = $v;
                $where['payStatus'] = 0;
                $order_no = $obj->where($where)->getField('order_no');

                //删除订单详情
                M('OrderDetail')->where('order_no="'.$order_no.'"')->delete();

                //删除订单
                $obj->where($where)->delete();
            }
            $state = 'SUCCESS';
            $message = 'Successful operation';
            $url = U('Order/index');
        }
        echo $this->echo_json_str($state,$message,$url);
    }
}
?>